//
//  SingleSelectionCellData.m
//  18Birdies
//
//  Created by Lc on 15/5/2.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import "LcFoundation.h"
#import "SingleSelectionCell.h"
#import "SingleSelectionCellData.h"

@implementation SingleSelectionCellData

+ (instancetype)cellDataWithTitle:(NSString*)title
                                        group:(NSString*)group
                                        value:(int)value
                                     selected:(BOOL)selected {
  LC_CHECK_NOT_EMPTY(title);
  return [self cellDataWithTitle:title
                        subtitle:nil
                           group:group
                           value:value
                        selected:selected];
}

+ (instancetype)cellDataWithTitle:(NSString*)title
                                     subtitle:(NSString*)subtitle
                                        group:(NSString*)group
                                        value:(int)value
                                     selected:(BOOL)selected {
  LC_CHECK_NOT_EMPTY(title);
  SingleSelectionCellData* cellData = [[self alloc] init];
  cellData->_title = title.copy;
  cellData->_subtitle = subtitle.copy;
  cellData.group = group;
  cellData.value = value;
  cellData.selected = selected;
  cellData.subtitleNumberOfLines = 1;
  cellData.clickable = YES;
  cellData.cellHeight = 44;
  cellData.onCellClickedHandler = ^(LcCellData* cellData) {
    LC_CHECK_NOT_NULL(cellData.cellAdapter);
    SingleSelectionCellData* singleSelectionCellData =
        (SingleSelectionCellData*)cellData;
    if (singleSelectionCellData.delegate != nil) {
      if ([singleSelectionCellData.delegate
              shouldSelectCellData:singleSelectionCellData]) {
        [singleSelectionCellData.cellAdapter
                   selectCellData:singleSelectionCellData
            deselectOthersInGroup:YES];
        [singleSelectionCellData.delegate
            onSingleSelectionChanged:singleSelectionCellData];
      }
    } else {
      [singleSelectionCellData.cellAdapter
                 selectCellData:singleSelectionCellData
          deselectOthersInGroup:YES];
    }
  };
  return cellData;
}

- (Class)cellClazz {
  return SingleSelectionCell.class;
}

- (NSString*)nibName {
  return @"SingleSelectionCell";
}

- (void)refreshCell:(SingleSelectionCell*)cell {
  [super refreshCell:cell];
  if (self.titleFont != nil) {
    [cell.titleLabel setLcFont:self.titleFont];
  } else {
    [cell.titleLabel setLcFont:[LcFont dr17]];
  }
  if (self.subtitle.length == 0) {
    cell.titleLabel.hidden = NO;
    cell.titleLabel.text = self.title;
    cell.title2Label.hidden = YES;
    cell.subtitleLabel.hidden = YES;
  } else {
    cell.titleLabel.hidden = YES;
    cell.title2Label.text = self.title;
    cell.title2Label.hidden = NO;
    cell.subtitleLabel.text = self.subtitle;
    cell.subtitleLabel.hidden = NO;
  }
  cell.subtitleLabel.numberOfLines = self.subtitleNumberOfLines;
  cell.checkerImageView.hidden = !self.selected;
  if (self.subtitle.length > 0) {
    float cellHeight =
        5 + cell.titleLabel.frame.size.height +
        [cell.subtitleLabel.text
            sizeOfFont:cell.subtitleLabel.font
              andWidth:[UIScreen mainScreen].bounds.size.width - 50 - 15]
            .height +
        5;
    self.cellHeight = MAX(cellHeight, 44);
  }
  
  cell.arrowImageView.hidden = !self.showRightArrow;
  if (self.showRightArrow) {
    cell.rightContentRightMargin.constant = -(cell.arrowWidthConstraint.constant + 8);
  } else {
    cell.rightContentRightMargin.constant = 0;
  }
  [cell.rightLabel setText:self.rightContent];
}

@end
